package com.techfort.metalrss;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.techfort.metalrss.core.LanguageItem;
import com.techfort.metalrss.db.FavouriteProvider;

public class Main extends Activity {
    
	FavouriteProvider fp;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        List<LanguageItem> langs = new ArrayList<LanguageItem>();
        
        langs.add(new LanguageItem(1, getResources().getDrawable(R.drawable.english), "English"));
        langs.add(new LanguageItem(2, getResources().getDrawable(R.drawable.german), "German"));
        langs.add(new LanguageItem(3, getResources().getDrawable(R.drawable.italian), "Italian"));
        
        ListView list = (ListView) findViewById(R.id.mainlist);
        
        list.setAdapter(new LanguageAdapter(this, R.layout.lang_option, langs));
        
        
        fp = new FavouriteProvider(Main.this);
        fp.open();
        

        
    }
    
    private class LanguageAdapter extends ArrayAdapter<LanguageItem>{

    	private List<LanguageItem> items;
    	
		public LanguageAdapter(Context context, int textViewResourceId,
				List<LanguageItem> objects) {
			super(context, textViewResourceId, objects);
			
			items = objects;
			
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			if(v==null){
				LayoutInflater li = (LayoutInflater) Main.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = li.inflate(R.layout.lang_option, null);
			}
			final int curpos = position;
			ImageView i = (ImageView) v.findViewById(R.id.flag);
			i.setImageDrawable(items.get(position).getFlag());
			
			TextView tv = (TextView) v.findViewById(R.id.lang);
			tv.setText(items.get(position).getLanguage());
			
			v.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(Main.this, ListChannels.class);
					Bundle extras = new Bundle();
					extras.putInt("LANG", items.get(curpos).getId());
					extras.putString("LANGUAGE", items.get(curpos).getLanguage());
					intent.putExtras(extras);
					Main.this.startActivity(intent);
				}
			});
			
			return v;
		}
    	
    	
    }
    
    @Override
    public void onStop() {
    	super.onStop();
    	if(fp!=null){
    		fp.close();
    	}
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	
    	MenuInflater mi = new MenuInflater(this);
    	mi.inflate(R.menu.simplemenu, menu);
    	
    	return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	switch(item.getItemId()){
    	case R.id.favourites:
    		Intent intent = new Intent(Main.this, Favourites.class);
    		startActivity(intent);
    	default:
    		return true;
    	}
    }
    
    
    
}